import socket
import struct
import zlib
from ApplicationLayer.Socket import Socket

class Mac:
    def __init__(self,dst_mac,src_mac,datatype,data,FCS):
        self.dst_mac = dst_mac  #六字节  目的mac
        self.src_mac = src_mac  #六字节  源mac
        self.datatype = datatype        #两字节  类型 08 00是IP
        self.data = data        # 数据部分
        self.FCS = FCS          #校验和4Bytes
        self.hash = 0   
    
    def getPack(self):
        header = struct.pack("!6s6s2s",self.dst_mac,self.src_mac,self.datatype)
        body = header+self.data
        res = self.crc32(body)
        # print(res)
        FCS = struct.pack("!I",res)
        # print(FCS)
        return body+FCS

    
    def crc32(self,body):
        """
        计算文件 crc32 hash 值
        """
        hash = zlib.crc32(body, self.hash)
        return hash


if __name__ == "__main__":
    myMac = Mac(dst_mac=b"\x00\x00\x00\x00\x00\x00",
                src_mac=b"\x00\x00\x00\x00\x00\x00",
                datatype=b"\x08\x00",
                data=b"",
                FCS=b"")
    res = myMac.getPack()
    print(res)
    mySocket = Socket("10.39.12.163",0)
    mySocket.send(res)
